package com.example.demo.controller.admin;

import com.example.demo.entity.User;
import com.example.demo.service.admin.UserAdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/admin/user")
@Controller
public class UserAdminController {

    @Autowired
    private UserAdminService userAdminService;

    /**
     * 添加用户 返回添加用户界面
     * 默认get请求
     * @return
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public String toAddUser() {

        return "/admin_addUser";
    }

    /**
     *添加用户
     *addUser的Post请求 根据需要调用相同方法名的不同请求方式
     * @return
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST, consumes = "application/json;charset=utf-8")
    @ResponseBody
    public Boolean addUser(@RequestBody User user) {
        if (user == null) {
            return false;
        }
        return userAdminService.addUser(user);
    }

    /**
     * 删除用户并跳转至查询用户界面
     *
     * @param id
     * @return String
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String toDelete(Integer id) {
        userAdminService.deleteUserById(id);
        return "redirect:selectUser";
    }

    /**
     * 根据id更新用户,跳转至更新用户界面
     *
     * @param id，model
     * @return String
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.GET)
    public String toUpdateUser(Integer id, Model model) {
        model.addAttribute("user", userAdminService.selectUserById(id));
        return "admin_updateUser";
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return Boolean
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST, consumes = "application/json;charset=utf-8")
    @ResponseBody
    public Boolean updateUser(@RequestBody User user) {
        if (user == null) {
            return false;
        }
        return userAdminService.addUser(user);
    }

    /**
     * 查询所有用户 返回用户列界面
     *
     * @param model
     * @return String
     */
    @RequestMapping(value = "/selectUser", method = RequestMethod.GET)
    public String toSelectUser(Model model) {
        model.addAttribute("user", userAdminService.selectAll());
        return "/admin_userList";
    }

    /**
     * 根据用户名查询用户
     *
     * @param userName
     * @return List<User>
     */
    @RequestMapping(value = "/toSelectByUsername", method = RequestMethod.GET)
    @ResponseBody
    public Integer toSelectByUsername(String userName) {
        Integer flag = 0;
        List<User> userList = userAdminService.selectByUsername(userName);
        if (userList.size() >= 1){
            flag = 1;
        }
        return flag;
    }

    /**
     * 根据用户名查询用户
     *
     * @param userName
     * @return List<User>
     */
    @RequestMapping(value = "/selectByUsername", method = RequestMethod.GET)
    @ResponseBody
    public List<User> selectByUsername(String userName) {
        List<User> user = userAdminService.selectByUsername(userName);
        return user;
    }

    /**
     * 查询所有用户
     *
     * @return List<User>
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public List<User> selectAll() {
        return userAdminService.selectAll();
    }

    /**
     * 分页写法
     *
     * @param pageNo
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    public String getPage(Integer pageNo, Integer pageSize, Model model) {
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 3;
        }
        PageInfo<User> pageInfo = userAdminService.getPage(pageNo, pageSize);
        model.addAttribute("page", pageInfo);
        return "/admin_userList";
    }


}
